/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.actions;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ipo.mm.web.util.BreadCrumbs;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class LocalizationAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String language = request.getParameter("language");
        String country = request.getParameter("country");

        if (null == country) {
            Locale locale = new Locale(language);
            setLocale(request, locale);
            request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
        } else {
            Locale locale = new Locale(language, country);
            setLocale(request, locale);
            request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
        }

        ActionForward forward = null;
        BreadCrumbs bc = (BreadCrumbs) request.getSession().getAttribute(ConstantManager.SESSION_BREADCRUMB_PARAM_NAME);
        // if user is not logged
        if (bc == null) {
            String defaultUrl = "/default.do";
            forward = new ActionForward(defaultUrl, false);
        } else {
            forward = bc.pop().getActionForward();
            forward.setRedirect(true);
        }



        return forward;
    }
}
